package org.hncors.gnssparse;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SelectInputTemplateDialog extends DialogFragment {
	
	private int mUniqueIdentifier = -1;
	private onCustomDialogInputListener mOnListener;
	private Button mButtonPositive;
	private TextView mInputTextView;
	

	public static SelectInputTemplateDialog newInstance(
			String title,
			int uniqueIdentifier,
			int txtInputType) {
		SelectInputTemplateDialog pcid = new SelectInputTemplateDialog();
		Bundle args = new Bundle();
		args.putString("SelectTemplateTitle", title);
		args.putInt("MultipleTemplateIdentifier", uniqueIdentifier);
		args.putInt("SelectTemplateInputType", txtInputType);
		pcid.setArguments(args);
		return pcid;
	}
	
	public static SelectInputTemplateDialog newInstance(
			String title,
			String defaultContent,
			int uniqueIdentifier,
			int txtInputType) {
		SelectInputTemplateDialog pcid = new SelectInputTemplateDialog();
		Bundle args = new Bundle();
		args.putString("SelectTemplateTitle", title);
		args.putInt("MultipleTemplateIdentifier", uniqueIdentifier);
		args.putInt("SelectTemplateInputType", txtInputType);
		args.putString("DefaultContent", defaultContent);
		pcid.setArguments(args);
		return pcid;
	}

	public interface onCustomDialogInputListener {

		public abstract void onCustomDialogInputListener(int uniqueIdentifier, String data);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle saveInstanceState) {
		 @SuppressWarnings("unchecked")
		 String title = getArguments().getString("SelectTemplateTitle");
		 mUniqueIdentifier = getArguments().getInt("MultipleTemplateIdentifier");
		 int txtInputType = getArguments().getInt("SelectTemplateInputType");
		 String defaultContet = getArguments().getString("DefaultContent");
		 
		 CustomAlertDialog.Builder builder = new CustomAlertDialog.Builder(getActivity())
					.setTitle(title)
					.setNegativeButton(getString(R.string.global_cancel), null)
					.setPositiveButton(getString(R.string.global_sure), new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (mInputTextView.getText().toString().length() == 0) return;
							dismiss();
							onSure();
						}
					});
		 
		 View view = getActivity().getLayoutInflater().inflate(R.layout.custom_select_dialog_edit, null);
		 setupUI(view, txtInputType, defaultContet);
		 builder.setView(view);
		 
		 return builder.create();
	}
	
	private void setupUI(View view, int inputType, String defaultContent) {
		if (view == null) return;
		mInputTextView = (TextView)view.findViewById(R.id.editTextSelectTemplate);
		if (defaultContent != null && defaultContent.length() != 0) 
			mInputTextView.setText(defaultContent);
		if (mInputTextView == null) return;
		if (inputType != -1) mInputTextView.setInputType(inputType);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mOnListener = (onCustomDialogInputListener)activity;
		} catch (ClassCastException e) {
			dismiss();
		}
	}
	
	private void onSure() {
		if (mInputTextView == null) return;
		String inputDataString = mInputTextView.getText().toString();
		if (mOnListener != null) mOnListener.onCustomDialogInputListener(mUniqueIdentifier, inputDataString);
	}
}
